//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "DayCounter.h"
using namespace Cephei::QL::Times;
#undef HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::Times::CDayCounter::CDayCounter () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phDayCounter = NULL;
#endif
        _ppDayCounter = new boost::shared_ptr<QuantLib::DayCounter> (new QuantLib::DayCounter ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::CDayCounter::CDayCounter (boost::shared_ptr<QuantLib::DayCounter>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDayCounter = NULL;
#endif
	_ppDayCounter = &childNative;
    
}
Cephei::QL::Times::CDayCounter::CDayCounter (QuantLib::DayCounter& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phDayCounter = NULL;
#endif
	_ppDayCounter = new boost::shared_ptr<QuantLib::DayCounter> (&childNative);
    
    _DayCounterOwner = owner;
    
}

Cephei::QL::Times::CDayCounter::CDayCounter (CDayCounter^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDayCounter = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppDayCounter = new boost::shared_ptr<QuantLib::DayCounter> (copy->GetShared());
        
    }
}
Cephei::QL::Times::CDayCounter::CDayCounter (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDayCounter = NULL;
#endif
	if (!t->IsSubclassOf(CDayCounter::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Times::CDayCounter::CDayCounter (QuantLib::Handle<QuantLib::DayCounter>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phDayCounter = &childNative;
	_ppDayCounter = &static_cast<boost::shared_ptr<QuantLib::DayCounter>>(childNative.currentLink());
    
    _DayCounterOwner = owner;
}
Cephei::QL::Times::CDayCounter::CDayCounter (QuantLib::Handle<QuantLib::DayCounter> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phDayCounter = &childNative;
	_ppDayCounter = &static_cast<boost::shared_ptr<QuantLib::DayCounter>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Times::CDayCounter::CDayCounter (QuantLib::DayCounter childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDayCounter = NULL;
#endif
	_ppDayCounter = new boost::shared_ptr<QuantLib::DayCounter> (new QuantLib::DayCounter (childNative));
    
}
#endif

Cephei::QL::Times::CDayCounter::~CDayCounter ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppDayCounter != NULL)
    {
	    delete _ppDayCounter;
        _ppDayCounter = NULL;
    }
}
Cephei::QL::Times::CDayCounter::!CDayCounter ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppDayCounter != NULL)
    {
	    delete _ppDayCounter;
    }
}
QuantLib::DayCounter& Cephei::QL::Times::CDayCounter::GetReference ()
{
    if (_ppDayCounter == NULL) throw REFNEW NativeNullException ();
	return **_ppDayCounter;
}
boost::shared_ptr<QuantLib::DayCounter>& Cephei::QL::Times::CDayCounter::GetShared ()
{
    if (_ppDayCounter == NULL) throw REFNEW NativeNullException ();
	return *_ppDayCounter;
}
QuantLib::DayCounter* Cephei::QL::Times::CDayCounter::GetPointer ()
{
    if (_ppDayCounter == NULL) throw REFNEW NativeNullException ();
	return &**_ppDayCounter;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::DayCounter>& Cephei::QL::Times::CDayCounter::GetHandle ()
{
	if (_phDayCounter == NULL)
	{
		_phDayCounter = new Handle<QuantLib::DayCounter> (*_ppDayCounter);
	}
	return *_phDayCounter;
}
#endif
bool Cephei::QL::Times::CDayCounter::HasNative () 
{
	return (_ppDayCounter != NULL);
}

Int64 Cephei::QL::Times::CDayCounter::DayCount (DateTime prm1, DateTime prm2)
{
    try
    {
        QuantLib::Date _prm1 = (QuantLib::Date)ValueHelper::Convert (prm1); //a
        QuantLib::Date _prm2 = (QuantLib::Date)ValueHelper::Convert (prm2); //a
    	QuantLib::BigInteger _rv = (QuantLib::BigInteger)(*_ppDayCounter)->dayCount ( _prm1,  _prm2 );   
        Int64 _nrv = (Int64)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Times::CDayCounter::Empty::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppDayCounter)->empty ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
String^ Cephei::QL::Times::CDayCounter::Name::get ()
{
    try
    {
    	std::string _rv = (std::string)(*_ppDayCounter)->name ( );   
        String^ _nrv = (String^)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Times::CDayCounter::YearFraction (DateTime prm1, DateTime prm2, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refPeriodStart, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refPeriodEnd)
{
    try
    {
        QuantLib::Date _prm1 = (QuantLib::Date)ValueHelper::Convert (prm1); //a
        QuantLib::Date _prm2 = (QuantLib::Date)ValueHelper::Convert (prm2); //a
        QuantLib::Date _refPeriodStart = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (refPeriodStart) ? (QuantLib::Date)ValueHelper::Convert (refPeriodStart->Value) : QuantLib::Date()); //9a
        QuantLib::Date _refPeriodEnd = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (refPeriodEnd) ? (QuantLib::Date)ValueHelper::Convert (refPeriodEnd->Value) : QuantLib::Date()); //9a
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppDayCounter)->yearFraction ( _prm1,  _prm2,  _refPeriodStart,  _refPeriodEnd );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Times::IDayCounter^ Cephei::QL::Times::CDayCounter_Factory::Create ()
{
    return REFNEW CDayCounter ();
}
